package com.ce.pms.occupation.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.occupation.dao.IPerfectItemTrackDao;
import com.ce.pms.occupation.model.PerfectItemTrackDO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
/**
 * 功能描述: 整改跟进DAO实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-30
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class PerfectItemTrackDaoImpl extends PmsBaseDaoImpl<PerfectItemTrackDO> implements
		IPerfectItemTrackDao {
	/**
	 * 方法描述: 分页查询所有整改跟进
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param SearchVO,perfectItemRequisitionId
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryPerfectItemTrackDOByPage(SearchVO searchVO,Long perfectItemRequisitionId){
		if(BlankUtil.isBlank(perfectItemRequisitionId)){
			return null;
		}
		StringBuilder sb = new StringBuilder("from PerfectItemTrackDO pt where pt.perfectItemRequisition.id = ? ");
		if (!BlankUtil.isBlank(searchVO.getOrderKey())
				&& !BlankUtil.isBlank(searchVO.getOrderValue())) {
			sb.append(" order by pt.").append(searchVO.getOrderValue()).append(
					" ").append(searchVO.getOrderKey());

		} else {
			sb
					.append(" order by pt.trackDate ");
		}
		return super.pagedQueryByPageNo(sb.toString(), searchVO.getPageNo(), searchVO.getPageSize(), new Object[]{perfectItemRequisitionId});
	}
	/**
	 * 方法描述: 查询某个项目中已经完工项目跟进记录
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemRequisitionId
	 * @return PerfectItemTrackDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemTrackDO queryCompletePerfectItemTrack(Long perfectItemRequisitionId){
		String queryStr = "from PerfectItemTrackDO pt where pt.perfectItemRequisition.id = ? and pt.completeYn = 1";
		Query query = super.createQuery(queryStr, perfectItemRequisitionId);
		return (query.list()==null?null:(query.list().size() < 1?null:(PerfectItemTrackDO)query.list().get(0)));
	}
	/**
	 * 方法描述: 查询某个项目中项目跟进记录
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemRequisitionId
	 * @return List<PerfectItemTrackDO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<PerfectItemTrackDO> queryPerfectItemTrackBy(Long perfectItemRequisitionId){
		String queryStr = "from PerfectItemTrackDO pt where pt.perfectItemRequisition.id = ? ";
		Query query = super.createQuery(queryStr, perfectItemRequisitionId);
		return query.list();
	}
	/**
	 * 方法描述: 查询某个项目中最大跟进日期方法
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemRequisitionId
	 * @return Date
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Date queryMaxDateOfPerfectItemTrack(Long perfectItemRequisitionId){
		String queryStr = "select MAX(pt.trackDate) from PerfectItemTrackDO pt where pt.perfectItemRequisition.id = ? ";
		Query query = super.createQuery(queryStr, perfectItemRequisitionId);
		Object result = query.uniqueResult();
		return (result == null ? null : (Date)result );
	}
	/**
	 * 方法描述: 批量提交整改跟进
	 * @author 胡煦
	 * Create on:2008-9-9
	 * @param List<Object[]> list
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean batchInsertPerfectItemTracks(List<Object[]> list){
		String sql = "insert into OM_PERFECT_ITEM_TRACK ( PERFECT_ITEM_REQUISITION_ID,EMPLOYEE_ID,TRACK_DATE,DESCRIPTION,COMPLETE_DATE,COMPLETE_YN ) values( ?,?,now(),'导入完工',?,'1')";
		this.executeSQLBatch(sql, list);
		return true;
	}
}
